{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Card:\n",
    "    def __init__(self, card_id, name, suit, rank, category):\n",
    "        \"\"\"\n",
    "        初始化卡牌对象\n",
    "        :param card_id: 卡牌ID\n",
    "        :param name: 牌名 (如 \"杀\", \"闪\", \"顺手牵羊\")\n",
    "        :param suit: 花色 (如 \"♦️\", \"♣️\", \"♥️\", \"♠️\")\n",
    "        :param rank: 点数 (如 6, 7, K)\n",
    "        :param category: 类别 (如 \"基本牌\", \"锦囊牌\", \"装备牌\")\n",
    "        \"\"\"\n",
    "        self.card_id = card_id\n",
    "        self.name = name\n",
    "        self.suit = suit\n",
    "        self.rank = rank\n",
    "        self.category = category\n",
    "\n",
    "    def __repr__(self):\n",
    "        return f\"Card({self.card_id}, {self.name}, {self.suit}, {self.rank}, {self.category})\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Card(1, 杀, ♦️, 6, 基本牌)\n",
      "Card(2, 杀, ♦️, 7, 基本牌)\n",
      "Card(3, 杀, ♦️, 8, 基本牌)\n",
      "Card(4, 杀, ♦️, 9, 基本牌)\n",
      "Card(5, 杀, ♦️, 10, 基本牌)\n",
      "Card(6, 杀, ♦️, K, 基本牌)\n",
      "Card(7, 杀, ♣️, 2, 基本牌)\n",
      "Card(8, 杀, ♣️, 3, 基本牌)\n",
      "Card(9, 杀, ♣️, 4, 基本牌)\n",
      "Card(10, 杀, ♣️, 5, 基本牌)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 读取文件并生成卡牌对象列表\n",
    "def load_cards_from_file(filename):\n",
    "    cards = []\n",
    "    try:\n",
    "        with open(filename, 'r', encoding='utf-8') as file:\n",
    "            for line in file:\n",
    "                # 去除行首尾空白字符\n",
    "                line = line.strip()\n",
    "                if line:\n",
    "                    # 分割每行数据\n",
    "                    data = line.split(',')\n",
    "                    # 提取数据并创建卡牌对象\n",
    "                    card_id = int(data[0])\n",
    "                    name = data[1]\n",
    "                    suit = data[2]\n",
    "                    rank = data[3]\n",
    "                    category = data[4]\n",
    "                    # 创建卡牌对象并添加到列表\n",
    "                    card = Card(card_id, name, suit, rank, category)\n",
    "                    cards.append(card)\n",
    "        return cards\n",
    "    except FileNotFoundError:\n",
    "        print(f\"文件 {filename} 未找到！\")\n",
    "        return []\n",
    "    except Exception as e:\n",
    "        print(f\"发生错误: {e}\")\n",
    "        return []\n",
    "\n",
    "# 加载卡牌数据\n",
    "cards = load_cards_from_file('军争160张牌.txt')\n",
    "\n",
    "# 输出部分卡牌查看\n",
    "for card in cards[:10]:  # 输出前10张卡牌\n",
    "    print(card)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "选择的卡牌是：Card(59, 闪, ♦️, J, 基本牌)\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 筛选卡牌函数：根据三元组（牌名，花色，点数）选择卡牌，选择ID号最小的那一张\n",
    "def filter_card_by_name_suit_rank(cards, name, suit, rank):\n",
    "    # 筛选出所有匹配的卡牌\n",
    "    filtered = [card for card in cards if card.name == name and card.suit == suit and card.rank == rank]\n",
    "    # 如果筛选出了多张相同的牌，选择ID最小的\n",
    "    if filtered:\n",
    "        return min(filtered, key=lambda card: card.card_id)\n",
    "    return None  # 如果没有找到匹配的卡牌，返回None\n",
    "\n",
    "# 示例：选择所有“闪”牌，花色为“♦️”，点数为“J”\n",
    "selected_card = filter_card_by_name_suit_rank(cards, \"闪\", \"♦️\", \"J\")\n",
    "if selected_card:\n",
    "    print(f\"选择的卡牌是：{selected_card}\")\n",
    "else:\n",
    "    print(\"未找到匹配的卡牌\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "charlatan",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
